package com.lv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lv.mapper.AdminMapper;
import com.lv.pojo.Admin;
import com.lv.pojo.LoginFrom;
import com.lv.service.AdminService;
import com.lv.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @Author:晓风残月Lx
 * @Date: 2022/4/28 18:48
 */
@Service("adminServiceImpl")
@Transactional
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    /**
     * 管理员登陆
     * @param loginFrom
     * @return
     */
    @Override
    public Admin login(LoginFrom loginFrom){
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",loginFrom.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginFrom.getPassword()));
        Admin admin = baseMapper.selectOne(queryWrapper);
        return admin;
    }

    /**
     * 管理员登录通过id验证
     * @param userId
     * @return
     */
    @Override
    public Admin getAdminById(long userId) {
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        Admin admin = baseMapper.selectOne(queryWrapper);
        return admin;
    }

    @Override
    public IPage<Admin> getAdminsByOpr(Page<Admin> pageParam, String adminName) {
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(adminName)){
            queryWrapper.like("name",adminName);
        }
        queryWrapper.orderByAsc("id");
        queryWrapper.orderByDesc("name");
        Page<Admin> adminPage = baseMapper.selectPage(pageParam, queryWrapper);
        return adminPage;
    }

    /**
     * 修改密码
     * @param usrId
     * @param oldPwd
     * @return
     */
    @Override
    public Admin updatePwd(Long usrId ,String oldPwd) {
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",usrId.intValue());
        queryWrapper.eq("password",oldPwd);
        Admin admin = getOne(queryWrapper);
        return admin;
    }
}
